$(function(){
	   var content ="subscribe";
	   var dataCount = 0;
		// 让 header 的 button 居中
		function btnCenter(){
				var vw = ($(window).width()-107)/2;
				var fw = ($(window).width()-160)/2;
				var rw = ($(window).width()-$(".remind").width())/2;
				$("header .btn-group").css("left",vw);
				$(".footerNav").css("left",fw);
				$(".remind").css("left",rw);
		};
		btnCenter();
		$(window).resize(function(){
			btnCenter();
		});		
		$("header .btn-group a").on("click",function(){
			$(this).css({backgroundColor:"white",color:"lightblue"}).siblings().css({backgroundColor:"lightblue",color:"white"});
			if($(this).attr("id")=="history"){
				$(".footerNav a").removeClass("disabled").removeClass("footerA").addClass("lightBgc");
				$(".footerNav a").eq(0).addClass("wcActive");
				content="history";
			}else{
				$(".footerNav a").removeClass("lightBgc").addClass("disabled").addClass("footerA");
				$(".footerNav a").eq(0).removeClass("wcActive");
			}
		});
		
		$("#goback").on("click",function(){
			history.go(-1);			
		});
		// 顶部删除按钮事件
		$("#delete").on({mousedown:function(){
			$(this).css("color","#999")
		},mouseup:function(){
			$(this).css("color","white");
		},click:function(){			
			if(!$(".sLeft").data("open")){				
				$(".sLeft").data("open",true).css("flex-basis","15%").find("span").fadeIn();				
			}else{				
				$(".sLeft").data("open",false).css("flex-basis",0).find("span").hide();				
			}
				$(".sRight").data("open",false).css("flex-basis",0).find("span").hide();									
		}})
		
		$("#data-content").on("click",".sLeft",function(){
			$(this).css("flex-basis",0).find("span").hide();
			$(this).parents("li").find(".sRight").data("open",true).css("flex-basis","20%").find("span").fadeIn();
		});
		
		// 回到顶部事件
		$("#backTop").click(function(event){
			$("html,body").animate({scrollTop:0},500)
		});
		// 到底部按钮事件
		$("#backBottom").click(function(){
			var vh = $("#data-content")[0].scrollHeight - $(window).height() + 44 ;			
			$("html,body").animate({scrollTop:vh},500);
		});
		
	/* *************** 请求********************** */
	
	// 删除记录请求
		$("#data-content").on("click",".sRight",function(){
			var $this = $(this);
			var data = $(this).parents("li").prop("data-history");
			if(confirm("确定删除?")){				
				$.ajax({
					type:"post",
					url:"/delete",
					data:data,
					async:true
				}).done(function(status,resData,jqXHR){
					if(resData.code == 1){					
						alert("删除成功");
						$this.hide();
					}
				}).fail(function(){
					alert("因为未知的错误，删除失败");
				});
			}else{
				$(this).data("open",false).css("flex-basis","0").find("span").hide();
				
			}
		});
		var htmlStr = "";
		// 请求页面默认数据(订阅的记录)
			$.ajax({
			type:"post",
			url:"/userHistory",
			async:true
		}).done(function(status,resData,jqXHR){
			if(resData){
				 htmlStr = template("mainTemplate",resData);
				$("#data-content").html(htmlStr);
				dataCount = resData.length;
				$(".dataCount").text(dataCount);
				$("body").css("overflow","scroll")
				$(".loading").show();
			}else{
				htmlStr = "<p class='remind'>没有订阅的历史记录</p>";
				$("#data-content").html(htmlStr);
				btnCenter();
				$("body").css("overflow","hidden");
			}
		}).fail(function(jqXHR,status,error){
				htmlStr = "<p class='remind'>系统出现未知的错误</p>";
				$("#data-content").html(htmlStr);
				btnCenter();			
				$("body").css("overflow","hidden");
				
		});
		
		// 点击订阅按钮显示页面第一次请求回来的数据(如果有的话)
		$("#subscribe").on("click",function(){
			$("#data-content").html(htmlStr);
					btnCenter();
					content="subscribe";
			
		});
		var readHtml ="";
		// 点击历史按钮请求注目(浏览)的历史记录
		$("#history").on("click",function(){
			
			if(readHtml){
				$("#data-content").html(readHtml);
					btnCenter();
			}else{
					$.ajax({
					type:"post",
					url:"/readWC",
					async:true
				}).done(function(status,resData,jqXHR){
					if(resData){				
						readHtml = template("mainTenplate",resData);
						$("#data-content").html(readHtml);
						$("body").css("overflow","scroll")
						$(".loading").show();
					}else{
						readHtml = "<p class='remind'>没有更多的浏览历史记录</p>";
						$("#data-content").html(readHtml);
						btnCenter();
						$("body").css("overflow","hidden");
					}
				}).fail(function(jqXHR,status,error){
					readHtml = "<p class='remind'>系统出现未知的错误</p>";
					$("#data-content").html(readHtml);
					btnCenter();
					$("body").css("overflow","hidden");
					
				});
			}

		});
		// 点击注目按钮显示浏览历史记录
		$(".readWC").on("click",function(){
			$(this).addClass("wcActive").siblings().removeClass("wcActive");
			$("#data-content").html(readHtml);
			content="history";
					btnCenter();
		})
		// 点击回复按钮显示回复的历史记录
		var replyHtml = "";
		$(".replyWC").on("click",function(){
			$(this).addClass("wcActive").siblings().removeClass("wcActive");
			content="reply";
			if(replyHtml){
				$("data-content").html(replyHtml);
				btnCenter();
			}else{
				$.ajax({
					type:"post",
					url:"/replyWC",
					async:true
				}).done(function(status,resData,jqXHR){
					if(resData){				
						replyHtml = template("mainTenplate",resData);
						$("#data-content").html(replyHtml);
						$("body").css("overflow","scroll")
						$(".loading").show();
					}else{
						replyHtml = "<p class='remind'>没有更多的回复历史记录</p>";
						$("#data-content").html(replyHtml);
						$("body").css("overflow","hidden");
						btnCenter();
						
					}
				}).fail(function(jqXHR,status,error){
					replyHtml = "<p class='remind'>系统出现未知的错误</p>";
					$("#data-content").html(replyHtml);
					btnCenter();
					$("body").css("overflow","hidden");
				});
			}
			btnCenter();
		});
		
		// 点击回复吐槽按钮显示吐槽过的记录
		var postHtml ="";
		$(".postWC").on("click",function(){
			$(this).addClass("wcActive").siblings().removeClass("wcActive");
			content="history";
			if(postHtml){
				$("#data-content").html(postHtml);
				btnCenter();
			}else{
				$.ajax({
					type:"post",
					url:"/postWC",
					async:true
				}).done(function(status,resData,jqXHR){
					if(resData){				
						postHtml = template("mainTenplate",resData);
						$("#data-content").html(postHtml);
						$("body").css("overflow","scroll")
						$(".loading").show();
					}else{
						postHtml = "<p class='remind'>没有更多的吐槽历史记录</p>";
						$("#data-content").html(postHtml);
						btnCenter();
						$("body").css("overflow","hidden");					
					}
				}).fail(function(jqXHR,status,error){
					postHtml = "<p class='remind'>系统出现未知的错误</p>";
					$("#data-content").html(postHtml);
					btnCenter();
					$("body").css("overflow","hidden");
				});
			}
					
			
		});	
		var totalHeight = $("#data-content")[0].scrollHeight+44;
		var dataCount = 0;
		// 上拉刷新
		var timer;
		$(window).scroll(function(){
			clearTimeout(timer);
			$(".shortcutBtn").fadeIn();
			timer = setTimeout(function(){
				$(".shortcutBtn").fadeOut();				
			},1000);
			var vx = $(window).height();
			var ws = $(window).scrollTop();
			var total = vx + ws;
			if(total>= totalHeight - 10){
				$(".loading").css({backgroundImage:"url(../images/loading_010.gif)",backgroundRepeat:"no-repeat",backgroundPosition: "50% 0"}).find("div").hide();	
				var data ={
					user:$.cookie("userId"),content,dataCount
				}
				$.ajax({
						type:"get",
						url:"/newHistory",
						async:true,
						data:data
					}).done(function(resData){
						if(resData){							
							htmlStr = template("mainTemplate",resData);
   							$("#data-content").append(htmlStr);
   							totalHeight = $("#data-content")[0].scrollHeight+44;
   							dataCount += dataCount+resData.length;
   							$(".dataCount").text(dataCount);
							$(".loading").css("background-image","").find("div").show().find(".iconM").show().next().text("上拉加载"); 						
						}else{							
							$(".loading").css("background-image","").find("div").show().html("<p>没有更多的数据</p>");					
						}
					}).fail(function(jqXHR,status,error){
							$(".loading").css("background-image","").find("div").show().html("<p><span class='glyphicon glyphicon-remove'></span>&nbsp;加载失败</p>");					
					})
			}
		});
});

